
var APP = {

	Player: function () {
		var loader = new THREE.ObjectLoader();
		var events = {};
		var dom = document.createElement( 'div' );
		this.dom = dom;
		this.width = 280;
		this.height = 250;
		this.load = function ( json ) {

			renderer = new THREE.WebGLRenderer( { antialias: true } );
			renderer.setClearColor( 0x000000 );
			renderer.setSize( 280, 250 );
			renderer.setPixelRatio( window.devicePixelRatio );

			var project = json.project;

			if ( project.gammaInput ) renderer.gammaInput = true;
			if ( project.gammaOutput ) renderer.gammaOutput = true;
			if ( project.shadows ) renderer.shadowMap.enabled = true;
			if ( project.vr ) renderer.vr.enabled = true;

			dom.appendChild( renderer.domElement );

			this.setScene( loader.parse( json.scene ) );
			this.setCamera( loader.parse( json.camera ) );

			//camera.matrixAutoUpdate = true;
			var orbit = new THREE.OrbitControls( camera, renderer.domElement );
			orbit.enableZoom = false;
			orbit.enableRotate = false;
			/*
			events = {
				init: [],
				start: [],
				stop: [],
				update: []
			};*/

			var scriptWrapParams = 'player,renderer,scene,camera';
			var scriptWrapResultObj = {};

			for ( var eventKey in events ) {
				scriptWrapParams += ',' + eventKey;
				scriptWrapResultObj[ eventKey ] = eventKey;
			}
			//dispatch( events.init, arguments );
			scene.background = new THREE.Color( 0xffffff );
			scene.rotation.y = -0.93; 
			camera.position.x = 16;
			camera.position.y = 4;
		};

		this.setCamera = function ( value ) {
			camera = value;
			camera.aspect = this.width / this.height;
			camera.updateProjectionMatrix();
			if ( renderer.vr.enabled ) {
				dom.appendChild( WEBVR.createButton( renderer ) );
			}
		};

		this.setScene = function ( value ) {
			scene = value;
		};
		
		this.setSize = function ( width, height ) {
			this.width = width;
			this.height = height;
			if ( camera ) {
				camera.aspect = this.width / this.height;
				camera.updateProjectionMatrix();
			}
			if ( renderer ) {
				renderer.setSize( width, height );
			}
		};
		/*
		function dispatch( array, event ) {
			for ( var i = 0, l = array.length; i < l; i ++ ) {
				array[ i ]( event );
			}
		}
		*/
		var time, prevTime;

		function animate() {
			/*
			time = performance.now();
			try {
				dispatch( events.update, { time: time, delta: time - prevTime } );
			} catch ( e ) {
				console.error( ( e.message || e ), ( e.stack || "" ) );
			}
			*/
			//scene.rotation.y += 0.005;
			renderer.render( scene, camera );
			prevTime = time;
		}

		this.play = function () {
			prevTime = performance.now();
			//dispatch( events.start, arguments );
			renderer.setAnimationLoop( animate );
		};

		this.dispose = function () {
			while ( dom.children.length ) {
				dom.removeChild( dom.firstChild );
			}
			renderer.dispose();
			camera = undefined;
			scene = undefined;
			renderer = undefined;
		};
	}
};
